import { song_lyric, check_music } from "@/network/api";
import http from "@/network/http";
export default {
  namespaced: true,
  state: {
    progress: 0.5,
    playing: false,
    currentIndex: -1, //当前播放下标
    playSongList: [], //当前播放列表
    duration: 0, //播放总时间
    isTouch: false, //是否可拖动
    playMode: 0, //播放模式
    currentTime: 0, //当前播放时间
    lyrics: [],
    currentPlayList: [],
    isPlay: true,
  },
  getters: {
    currentSong(state) {
      return state.currentIndex >= 0
        ? state.playSongList[state.currentIndex]
        : {};
    },
    currentLyric(state) {
      let index = state.lyrics.findIndex(
        (item) => item.time > state.currentTime
      );
      if (index > 0) {
        return index - 1;
      } else if (index == 0) {
        return 0;
      } else {
        return state.lyrics.length - 1;
      }
    },
  },
  mutations: {
    setProgress(state, payload) {
      state.progress = payload;
    },
    setPlaying(state, payload) {
      state.playing = payload;
    },
    //修改当前歌曲
    setCurrentIndex(state, payload) {
      if (payload == "pre") {
        state.currentIndex--;
        state.currentIndex < 0 &&
          (state.currentIndex = state.playSongList.length - 1);
      } else if (payload == "next") {
        state.currentIndex++;
        state.currentIndex > state.playSongList.length - 1 &&
          (state.currentIndex = 0);
      } else {
        state.currentIndex = payload;
      }
    },
    setPlaySongList(state, payload) {
      state.playSongList = payload;
    },
    setDuration(state, payload) {
      state.duration = payload;
    },
    setIsTouch(state, payload) {
      state.isTouch = payload;
    },
    setPlayMode(state) {
      state.playMode >= 2 ? (state.playMode = 0) : state.playMode++;
    },
    setCurrentTime(state, payload) {
      state.currentTime = payload;
    },
    getRandomSong(state) {
      while (true) {
        let index = ~~(Math.random() * state.playSongList.length);
        if (index != state.currentIndex) {
          state.currentIndex = index;
          break;
        }
      }
    },
    setLyrics(state, payload) {
      state.lyrics = payload;
    },
    setCurrentPlayList(state, payload) {
      if (
        state.currentPlayList.findIndex((item) => item.id == payload.id) != -1
      )
        return;
      state.currentPlayList.unshift(payload);
    },
    delCurrentPlay(state, payload) {
      state.currentPlayList.splice(payload, 1);
    },
    setIsPlay(state, payload) {
      state.isPlay = payload;
    },
  },
  actions: {
    async getLyric(context) {
      let {
        lrc: { lyric },
      } = await http.get(song_lyric, { id: context.getters.currentSong.id });
      lyric = lyric.split("]");
      let word = [],
        strTime = [];
      lyric.map((item) => {
        let [lycw, lyct] = item.split("[");
        word.push(lycw);
        strTime.push(lyct);
      });
      word.shift();
      strTime.pop();
      let time = strTime.map((item) => {
        let [mm, ss] = item.split(":");
        return mm * 60 + +ss;
      });
      let lyrics = time.map((item, i) => ({
        time: item,
        strTime: strTime[i],
        word: word[i],
      }));
      context.commit("setLyrics", lyrics);
    },
    async checkMusic(context, payload) {
      return await http.get(check_music, { id: payload });
    },
  },
};
